package com.li.controller;


import com.li.mapper.RoleDeptMapper;
import com.li.pojo.dto.RoleDto;
import com.li.pojo.dto.RoleDeptDTO;
import com.li.pojo.dto.RolePageDto;
import com.li.pojo.po.PageResult;
import com.li.pojo.po.Result;
import com.li.pojo.po.Role;
import com.li.pojo.vo.RoleVO;
import com.li.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-04-13
 */
@RestController
@Slf4j
@RequestMapping("/system/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleDeptMapper roleDeptMapper;

    //角色添加
    @PostMapping()
    public Result addRole(@RequestBody RoleDto roleDto){
        log.info("{}",roleDto);
        roleService.addRole(roleDto);
        return Result.success();
    }

    //分页查询
    @GetMapping("/list")
    public PageResult findByPage(RolePageDto rolePageDto){
        PageResult page=roleService.findByPage(rolePageDto);
        return page;
    }

    //根据id查询角色
    @GetMapping("/{roleId}")
    public Result findById(@PathVariable Integer roleId){
        Role role =roleService.findById(roleId);

        return Result.success(role);
    }

    //根据ids删除角色
    @DeleteMapping("/{roleIds}")
    public Result deleteByIds(@PathVariable List<Integer> roleIds){
        roleService.deleteByIds(roleIds);
        return Result.success();
    }

    //查询全部
    @GetMapping("/optionselect")
    public Result findAll(Integer deptId){
        List<Role> list=roleService.getAll();
        return Result.success(list);
    }

    //修改状态
    @PutMapping("/changeStatus")
    public Result changeStatus(@RequestBody RoleDto roleDto){
        roleService.updateRole(roleDto);
        return Result.success();
    }

    //修改权限
    @PutMapping("/dataScope")
    public Result dataScope(@RequestBody String s) throws JSONException {
        roleService.dataScope(s);

//        roleService.updateDateScope(roleDto);
        return Result.success();
    }


    //修改角色
    @PutMapping
    public Result updateRole(@RequestBody RoleDto roleDto){
        roleService.updateRole(roleDto);
        return Result.success();
    }

}
